package com.atguigu.eduservice.service;

import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.impl.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.impl.EduChapterService;

/**
 * @author cyt
 * @create 2020-05-07 10:43
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    EduChapterMapper eduChapterMapper;

    @Autowired
    EduVideoService eduVideoService;

    // 查询章节
    @Override
    public EduChapter getChapterById(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        EduChapter chapter = eduChapterMapper.selectOne(wrapper);
        return chapter;
    }

    //删除章节
    @Override
    public boolean deleteChapter(String id) {

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);

        int count = eduVideoService.count(wrapper);

        if (count > 0) {
            throw new GuliException(20001, "不能删除");
        } else {
            int deleteById = baseMapper.deleteById(id);

            return deleteById > 0;
        }

    }

}
